//
//  MYGpatientImageDetailVC.swift
//  MYDoctor
//
//  Created by Denny on 2024/5/6.
//

import UIKit

class MYGpatientImageDetailVC: MYGBaseController {
    var model = MYGPatientImages()
    var selectType = 0
    lazy var headerView: MYGDetailHeaderView = {
        let control = Bundle.main.loadNibNamed("MYGDetailHeaderView", owner: nil)?.first as! MYGDetailHeaderView
        control.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: 76)
        control.tapFilterCompletion = {[weak self] in
            self?.showFilterView()
        }
        return control
    }()
    var pid = 0
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 4
        layout.minimumInteritemSpacing = 2
        var leftMargin = 20.0
        var midMargin = 4.0
        let itemWidth = (screenWidth - 2 * leftMargin - 2 * midMargin) / 3
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        let control = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        control.register(MYGImageDetailCell.self, forCellWithReuseIdentifier: "MYGImageDetailCell")
        control.showsHorizontalScrollIndicator = false
        control.showsVerticalScrollIndicator = false
        control.backgroundColor = .white
        control.delegate = self
        control.dataSource = self
        control.contentInset = UIEdgeInsets(top: 0, left: leftMargin, bottom: 0, right: leftMargin)
        return control
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "检查影像"
        let rightBar = UIBarButtonItem(image: UIImage.init(named: "Myg_addImages"), style: .plain, target: self, action: #selector(tapAddImage))
        self.navigationItem.rightBarButtonItem = rightBar
        setUpUI()
        getData()
    }
    func setUpUI() {
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(76)
        }
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(headerView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    func getData() {
        MYGLoading.show()
        self.headerView.isHidden = true
        MYGNetwork<MYGPatientImageApi,MYGPatientImages>.request(target: .examinationDetail(id: pid)) {[weak self] item in
            self?.model = item
            self?.reloadUI()
            MYGLoading.hide()
       } failed: { _, error in
           MYGLoading.hide()
           MMYGHUD.showToast(text: error)
       }
    }
    
    func reloadUI() {
        self.headerView.isHidden = false
        self.headerView.model = self.model
        self.collectionView.reloadData()
    }
    
    func showFilterView() {
        self.popup.bottomSheet(with: true, bgColor: .black.withAlphaComponent(0.43)) {
            let actionSheet = MYGPatientImageLayoutAlert.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: 160 + bottomSafeheight))
            actionSheet.tapLayoutIndex = {[weak self]index in
                self?.reloadCollectionViewLayout(index: index)
            }
            
            actionSheet.didSelectIndex(index: self.selectType)
            return actionSheet
        }
    }
    
    func reloadCollectionViewLayout(index:Int) {
        self.selectType = index
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        let leftMargin = 20.0
        let midMargin = 4.0
        layout.minimumLineSpacing = 4
        layout.minimumInteritemSpacing = 2
        if index == 0 {
            //标准
            let itemWidth = (screenWidth - 2 * leftMargin - 2 * midMargin) / 3
            layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        }else if index == 1 {
            //缩略
            let itemWidth = (screenWidth - 2 * leftMargin - 5 * midMargin) / 6
            layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        }else {
            //列表
            let itemWidth = screenWidth - 2 * leftMargin
            layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        }
        self.collectionView.contentInset = UIEdgeInsets(top: 0, left: leftMargin, bottom: 0, right: leftMargin)
        self.collectionView.setCollectionViewLayout(layout, animated: true)
        self.collectionView.reloadData()
        
    }
   
    @objc func tapAddImage() {
        self.popup.dialog(with: true, bgColor: .black.withAlphaComponent(0.43)) {
            let actionSheet = Bundle.main.loadNibNamed("MYGAddPatientImageAlert", owner: nil)?.first as! MYGAddPatientImageAlert
            actionSheet.tapCopyCompletion = {text in
                let pasteboard = UIPasteboard.general
                pasteboard.string = text
                JFPopupView.popup.toast(hit: "已复制到剪切板")
                self.popup.dismissPopup()
            }
            actionSheet.layer.cornerRadius = 12
            actionSheet.layer.masksToBounds = true
            actionSheet.frame = CGRect.init(x: 0, y: 0, width: 315, height: 191)
            return actionSheet
        }
    }
}
extension MYGpatientImageDetailVC:UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGImageDetailCell", for: indexPath) as! MYGImageDetailCell
        cell.icon.sd_setImage(with: URL(string: self.model.value[indexPath.row]))
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model.value.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var images = [SKPhoto]()
        for (_,obj)in self.model.value.enumerated() {
            let photo = SKPhoto.photoWithImageURL(obj)
            photo.shouldCachePhotoURLImage = true
            images.append(photo)
        }
        SKPhotoBrowserOptions.displayAction = false
        let browser = SKPhotoBrowser.init(photos: images, initialPageIndex: indexPath.row)
        self.present(browser, animated: true)
    }
}
